257C - View Angle - CodeForces Solution


brute force geometry math *1800

Please click on ads to support us..

Python Code:

import math as m
n = int(input())

a = [] 
for i in range(n):
    x,y = map(int,input().split())
    if x==0 and y==0:
        theta = 0 
        
    elif x==0 or y==0:
        if x==0:
            if y>0:
                theta = 90
                
            else:
                theta = 270
                
        if y==0:
            if x>0:
                theta = 0
                
            else:
                theta = 180
                
                
    elif x>=0 and y>=0:
        theta = 180*m.atan(y/x)/m.pi
        
    elif x>=0 and y<0:
        theta = 360+180*m.atan(y/x)/m.pi
        
    elif x<0 and y>=0:
        theta = 180+180*m.atan(y/x)/m.pi
    else:
        theta = 180+180*m.atan(y/x)/m.pi
    
    a.append(theta)
    

a = sorted(a)
m = len(a)  
mx = 0 
for i in range(1,m):
    mx = max(mx,a[i]-a[i-1])
y = 360-a[-1]
x = a[0]
mx = max(mx,x+y)

print(360-mx)

C++ Code:

// Problem: C. View Angle
// Contest: Codeforces - Codeforces Round #159 (Div. 2)
// URL: https://codeforces.com/contest/257/problem/C
// Memory Limit: 256 MB
// Time Limit: 2000 ms

/*
	Author: MikiMiku
	
	Observation:
	
	Idea: 
		
*/

#include <bits/stdc++.h>
using namespace std;

//#define int long long
#define endl '\n'

#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back 
#define eb emplace_back 
#define mp make_pair

#define isOn(S, j) (S & (1 << j))
#define setBit(S, j) (S |= (1 << j))
#define clearBit(S, j) (S &= ~(1 << j))
#define toggleBit(S, j) (S ^= (1 << j))
#define LSB(S) (S & (-S))
#define setAll(S, n) (S = (1 << n) - 1)
#define modulo(S, N) ((S) & (N - 1))   // returns S % N, where N is a power of 2
#define isPowerOfTwo(S) (!(S & (S - 1)))
#define nearestPowerOfTwo(S) ((int)pow(2.0, (int)((log((double)S) / log(2.0)) + 0.5)))
#define turnOffLastBit(S) ((S) & (S - 1))
#define turnOnLastZero(S) ((S) | (S + 1))
#define turnOffLastConsecutiveBits(S) ((S) & (S + 1))
#define turnOnLastConsecutiveZeroes(S) ((S) | (S - 1))

typedef complex<double> point;

typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;

const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const int FMOD = 998244353; 
const ll INF = 1e9;
const ld EPS = 1e-9;
const double PI=acos(-1);

#define fi first
#define se second
typedef pair<int, int> ii;  
typedef vector<ii> vii;
typedef vector<int> vi;
typedef map<int,int> mii; 

//FLOAT PRECISION SETTINGS
/*
cout.setf(ios::fixed,ios::floatfield);
cout.precision(3);
*/
//FILE IO
void setIO(string s) {
	freopen((s + ".in").c_str(), "r", stdin);
	freopen((s + ".out").c_str(), "w", stdout);
}

//........................................................................

signed main() {
	ios_base::sync_with_stdio(0);cin.tie(nullptr); cout.tie(nullptr);
	int n;
	cin >> n;
	
	if(n == 1) {
		cout << 0 << endl;
		return 0;
	}
	
	vector<pair<double, pair<double, double>>> P;
	for(int i = 0; i < n; ++i) {
		double x, y;
		cin >> x >> y;
		if(x > 0 && y >= 0) {
			P.pb({atan(abs(y)/abs(x)), {x, y}});
		} else if(x <= 0 && y > 0) {
			if(x == 0) P.pb({0.5*PI, {x, y}});
			else P.pb({atan(abs(x)/abs(y)) + 0.5*PI, {x, y}});
		} else if(x < 0 && y <= 0) {
			P.pb({atan(abs(y)/abs(x)) + PI, {x, y}});
		} else {
			if(x == 0) P.pb({1.5*PI, {x, y}});
			else P.pb({atan(abs(x)/abs(y)) + 1.5*PI, {x, y}});
		}
	}
	sort(all(P));
	
	double ans = 360;
	for(int i = 1; i <= n; ++i) {
		double x1, y1, x2, y2;
		x1 = P[i - 1].se.fi; y1 = P[i - 1].se.se;
		x2 = P[i%n].se.fi; y2 = P[i%n].se.se;
		
		//cerr << x1 << " " << y1 << " ";
		
		/*double dot = (x1*x2) + (y1*y2);
		double d1 = hypot(x1, y1);
		double d2 = hypot(x2, y2);
		
		double res = dot/(d1*d2);
		double deg = (acos(res)/PI)*180;*/
		//cerr << P[i%n].fi << " " << P[i - 1].fi << " ";
		double deg = ((P[i%n].fi - P[i - 1].fi)/PI)*180;
		//cerr << deg << endl;
		if(deg < 0) deg += 360;
		
		
		ans = min(360 - deg, ans);
		if(n == 2) ans = min(deg, ans);
	}
	if(ans == 360) ans = 0;
	
	cout.setf(ios::fixed,ios::floatfield);
	cout.precision(9);
	
	/*if(ans < EPS) {
		bool colinear = true;
		for(int i = 1; i < n; ++i) {
			if(fabs(P[i - 1].fi - P[i].fi) >= EPS) {
				colinear = false;
				break;
			}
		}
		if(!colinear) ans += 360;
	}*/
	
	cout << ans << endl;
	return 0;
}


Comments

Submit
0 Comments
More Questions

1131F - Asya And Kittens
1475F - Unusual Matrix
133B - Unary
1547A - Shortest Path with Obstacle
624A - Save Luke
1238A - Prime Subtraction
1107C - Brutality
1391B - Fix You
988B - Substrings Sort
312A - Whose sentence is it
513A - Game
1711E - XOR Triangle
688A - Opponents
20C - Dijkstra
1627D - Not Adding
893B - Beautiful Divisors
864B - Polycarp and Letters
1088A - Ehab and another construction problem
1177B - Digits Sequence (Hard Edition)
1155B - Game with Telephone Numbers
1284A - New Year and Naming
863B - Kayaking
1395B - Boboniu Plays Chess
1475D - Cleaning the Phone
617B - Chocolate
1051B - Relatively Prime Pairs
95B - Lucky Numbers
1692D - The Clock
1553D - Backspace
1670D - Very Suspicious